﻿using FarseerPhysics.Dynamics.Contacts;
using IcicleFramework.Components.Physics;
using IcicleFramework.GameServices;
using IcicleFramework.Physics;

namespace IcicleFramework.Components.Behaviors.CollisionBehaviors
{
    public class PhysicsCollisionBehavior : BaseBehavior
    {
        public void OnCollision(IPhysicsComponent source, IPhysicsComponent collided, Contact contact)
        {
            //The source has collided with the 'collided' object. Make sure that we belong to the same parent as the source
            //object and then fire our action!
            if (ParentGameObject.GUID.Equals(source.Parent.GUID))
            {
                Parent.FireSingleAction(collided.Parent, ActionName);
            }
        }

        public override void Initialize()
        {
            //Find the Physics game service and register ourselves to track collision events for our IGameObject.
            IPhysicsManager physicsManager = GameServiceManager.GetService<IPhysicsManager>();

            if (physicsManager != null && ParentGameObject!= null)
                physicsManager.SubscribeCollisionEvent(ParentGameObject.GUID, OnCollision);
        }

        public override IBehavior DeepClone()
        {
            return new PhysicsCollisionBehavior();
        }
    }
}
